package com.edushare.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;

import com.edushare.entity.Group;
import com.edushare.entity.Member;

public class GroupController {

	private class CreateGroup extends
			AsyncTask<List<NameValuePair>, Void, JSONObject> {
		@Override
		protected JSONObject doInBackground(List<NameValuePair>... params) {
			JSONObject json = jsonParser.makeHttpRequest(createGroupURL, "POST",
					params[0]);
			return json;
		}
	}

	private class GetMembers extends
			AsyncTask<List<NameValuePair>, Void, JSONObject> {
		@Override
		protected JSONObject doInBackground(List<NameValuePair>... params) {
			JSONObject json = jsonParser.makeHttpRequest(retrieveMembersURL,
					"GET", params[0]);
			return json;
		}
	}

	private class RetrieveGroups extends
			AsyncTask<List<NameValuePair>, Void, JSONObject> {
		@Override
		protected JSONObject doInBackground(List<NameValuePair>... params) {
			JSONObject json = jsonParser.makeHttpRequest(retrieveGroupsURL,
					"GET", params[0]);
			return json;
		}
	}

	private class UpdateGroupIcon extends
			AsyncTask<List<NameValuePair>, Void, JSONObject> {
		@Override
		protected JSONObject doInBackground(List<NameValuePair>... params) {
			JSONObject json = jsonParser.makeHttpRequest(updateGroupIconURL,
					"POST", params[0]);
			return json;
		}
	}

	// JSON parser class
	JSONParser jsonParser;

	private JSONController jController = new JSONController();

	private static String retrieveGroupsURL = "http://unifiq.5kportal.com/debbieunifiq/retrieve_groups.php";
	private static final String updateGroupIconURL = "http://unifiq.5kportal.com/debbieunifiq/update_group_icon.php";
	private static String retrieveMembersURL = "http://unifiq.5kportal.com/debbieunifiq/get_members.php";
	private static String createGroupURL = "http://unifiq.5kportal.com/debbieunifiq/insert_new_group.php";

	private static final String TAG_SUCCESS = "success", TAG_GROUP = "group",
			TAG_GROUPID = "groupID", TAG_NAME = "name",
			TAG_DESCRIPTION = "description", TAG_IMAGEPATH = "imagePath",
			TAG_MEMBER = "member", TAG_USERID = "userID", TAG_EMAIL = "email",
			TAG_PROFILEPIC = "profilePic";

	// constructor
	public GroupController() {
		jsonParser = new JSONParser();
	}

	public boolean createGroup(String groupName, String groupDesc,
			String imageName, String groupPwd, String userID) {
		boolean isCreated = false;

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("groupName", groupName));
		params.add(new BasicNameValuePair("groupDesc", groupDesc));
		params.add(new BasicNameValuePair("imagePath",
				"http://unifiq.5kportal.com/images/post_images/" + imageName));
		params.add(new BasicNameValuePair("groupPwd", groupPwd));
		params.add(new BasicNameValuePair("userID", userID));

		JSONObject json = new JSONObject();
		try {
			json = new CreateGroup().execute(params).get();
			int success = json.getInt(TAG_SUCCESS);
			if (success == 1) {
				isCreated = true;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return isCreated;
	}

	public ArrayList<Member> getMembers(int groupID) {
		ArrayList<Member> memberList = new ArrayList<Member>();
		JSONArray MemberJArr = new JSONArray();

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG_GROUPID, String.valueOf(groupID)));

		JSONObject json = new JSONObject();
		try {
			json = new GetMembers().execute(params).get();
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				MemberJArr = json.getJSONArray(TAG_MEMBER);

				for (int i = 0; i < MemberJArr.length(); i++) {
					JSONObject jObject2 = MemberJArr.getJSONObject(i);

					int userID = jObject2.getInt(TAG_USERID);
					String name = jObject2.getString(TAG_NAME);
					String email = jObject2.getString(TAG_EMAIL);
					String profilePic = jObject2.getString(TAG_PROFILEPIC);

					Member Member = new Member();

					Member.setUserID(userID);
					Member.setEmail(email);
					Member.setGroupID(groupID);
					Member.setName(name);
					Member.setProfilePic(profilePic);

					memberList.add(Member);
				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return memberList;
	}

	public ArrayList<Group> retrieveGroups() {
		ArrayList<Group> groupsList = new ArrayList<Group>();
		JSONArray groupJArr = new JSONArray();

		List<NameValuePair> params = new ArrayList<NameValuePair>();

		JSONObject json = new JSONObject();
		try {
			json = new RetrieveGroups().execute(params).get();
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				groupJArr = json.getJSONArray(TAG_GROUP);

				for (int i = 0; i < groupJArr.length(); i++) {
					JSONObject jObject = groupJArr.getJSONObject(i);

					int groupID = jObject.getInt(TAG_GROUPID);
					String name = jObject.getString(TAG_NAME);
					String description = jObject.getString(TAG_DESCRIPTION);
					String imagePath = jObject.getString(TAG_IMAGEPATH);
					int userID = jObject.getInt("userID");
					String password = jObject.getString("password");

					Group gp = new Group();

					gp.setGroupID(groupID);
					gp.setDescription(description);
					gp.setImagePath(imagePath);
					gp.setName(name);
					gp.setUserID(userID);
					gp.setPassword(password);

					groupsList.add(gp);

				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return groupsList;
	}

	public void updateGroupIcon(int groupID, String imagename) {

		String url = "http://unifiq.5kportal.com/images/post_images/"
				+ imagename;

		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG_IMAGEPATH, url));
		params.add(new BasicNameValuePair(TAG_GROUPID, String.valueOf(groupID)));

		JSONObject json = new JSONObject();
		try {
			json = new UpdateGroupIcon().execute(params).get();
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				// successfully updated
			} else {
				// failed to update product
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
